#include <stdlib.h>
#include <stdio.h>
int binary_search(const char *a, int n, int key, size_t s, int is_front)
{
    int mid, front = 0, back = n - 1;
    int v, v2;
    while (front <= back)
    {
        mid = (front + back) / 2;
        v = *(int*)(a + mid * s);
        if (v == key)
            return mid * s;
        if (v < key)
            front = mid + 1;
        else back = mid - 1;
    }
    v = *(int*)(a + back * s);
    v2 = *(int*)(a + front * s);
    if (is_front && v < key && v2 > key)
    {
        return (is_front < 0 ? front : back) * s;
    }
    return -1;
}
